package com.itest.controller;
import com.itest.entity.User;
import com.itest.entity.Case;
import com.itest.dao.CaseMapper;
import com.itest.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/*
 * 魏建轩 赵金龙 魏巍
 * */

@Controller
@RequestMapping(value = "/caseService")
public class CaseController {
    @Autowired
    private CaseMapper case_dao;

    @RequestMapping(value = "/rename")
    @ResponseBody
    @CrossOrigin
    public CommonResult rename(
            @RequestBody User user
    ){
        String name = user.getUsername();
        Integer id = user.getUser_id();
        CommonResult cr = new CommonResult();
        Integer res = case_dao.renameCase(name,id);
        if(res!=null){
            cr.setCode(0);//代表成功
            cr.setMsg("重命名case成功");
            cr.setData("");
        }else {
            cr.setCode(1);
            cr.setMsg("重命名case失败");
            cr.setData("");
        }

        return cr;

    }

    //    新建  插入 返回case 的 id
    @ResponseBody
    @RequestMapping(value = "/createCase")
    @CrossOrigin
    public CommonResult createCase(

            @RequestBody Case cas
    ){
        Integer cid = cas.getCid();

        CommonResult cr = new CommonResult();

        Integer res = case_dao.insertCase(cas);
        if(res!=null){
            cr.setCode(0);//代表成功
            cr.setMsg("创建case成功");
            cr.setData(cas);
        }else {
            cr.setCode(1);
            cr.setMsg("创建case失败");
            cr.setData("");
        }

        return cr;
    }

    //    删除
    @ResponseBody
    @RequestMapping(value = "/delete")
    @CrossOrigin
    public CommonResult deleteCase(
            @RequestBody Case cas
    ){
        CommonResult cr = new CommonResult();
        Integer res = case_dao.deleteCase(cas.getId());
        if(res!=null){
            cr.setCode(0);//代表成功
            cr.setMsg("删除case成功");
            cr.setData("");
        }else {
            cr.setCode(1);
            cr.setMsg("删除case失败");
            cr.setData("");
        }

        return cr;
    }


}
